/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.service.loan;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.drew.lang.StringUtil;
import com.server.common.persistence.Page;
import com.server.common.service.CrudService;
import com.server.modules.sys.entity.loan.LoanMaterialtypeRe;
import com.server.modules.sys.dao.loan.LoanMaterialtypeReDao;

/**
 * 贷款产品资料关系表管理Service
 * @author 王磊
 * @version 2016-09-08
 */
@Service
@Transactional(readOnly = true)
public class LoanMaterialtypeReService extends CrudService<LoanMaterialtypeReDao, LoanMaterialtypeRe> {

	public LoanMaterialtypeRe get(String id) {
		return super.get(id);
	}

	public List<LoanMaterialtypeRe> findList(LoanMaterialtypeRe loanMaterialtypeRe) {
		return super.findList(loanMaterialtypeRe);
	}

	public Page<LoanMaterialtypeRe> findPage(Page<LoanMaterialtypeRe> page, LoanMaterialtypeRe loanMaterialtypeRe) {
		return super.findPage(page, loanMaterialtypeRe);
	}

	@Transactional(readOnly = false)
	public void save(LoanMaterialtypeRe loanMaterialtypeRe) {
		super.save(loanMaterialtypeRe);
	}

	@Transactional(readOnly = false)
	public void delete(LoanMaterialtypeRe loanMaterialtypeRe) {
		super.delete(loanMaterialtypeRe);
	}

	/**
	 * 根据loanId批量保存该借款的资料类型
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public boolean saveByLoanId(String loanId,String[] materialtypeIds ) {
		//参数校验
		if(StringUtils.isBlank(loanId)||materialtypeIds==null){
			return false;
		}
		//先删除之前的关系 再保存（避免出现重复插入的现象  这样效率稍微低一点，但这种操作不会太频繁，可以这样）
		this.deleteByLoanId(loanId);
		for (String materialtypeId : materialtypeIds) {
			LoanMaterialtypeRe loanMaterialtypeRe=new LoanMaterialtypeRe();
			loanMaterialtypeRe.setLoanId(loanId);
			loanMaterialtypeRe.setMaterialTypeId(materialtypeId);
			this.save(loanMaterialtypeRe);
		}
		return true;
	}

	/**
	 * 根据loanId批量删除该借款的资料类型
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public boolean deleteByLoanId(String loanId ) {
		//参数校验
		if(StringUtils.isBlank(loanId)){
			return false;
		}
		return dao.deleteByLoanId(loanId)>0?true:false;
	}
	
}